﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2018 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Synchronized windows</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" />
  <script type="text/javascript" src="https://openlayers.org/en/latest/build/ol.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <!-- Synchronize script -->
  <script type="text/javascript" src="../../dist/extra/WSynchro.js"></script>
  
  <link rel="stylesheet" href="../style.css" />

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Synchronized windows</h1>
  </a>
  <div class="info">
    This example synchronize maps in different windows. 
    <br />
    The WSynchro object do the job: WSynchro.synchronize will dispatch a synchro event.
    <br />
    <button onclick="openWin();">Open a new window!</button> Synchro: <span id="nb"></span>
  </div>

  <!-- map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <script type="text/javascript">

  // Two base layers
  var stamen = new ol.layer.Tile({
    title: "Watercolor",
    baseLayer: true,
    source: new ol.source.Stamen({
      layer: 'watercolor'
    })
  });

  // The map
  var z = 6; pt = [270148, 6247782]; rot = 0;
  // Window has a source > get its position
  if (WSynchro.source) {
    z = WSynchro.source.map.getView().getZoom();
    pt = WSynchro.source.map.getView().getCenter();
    rot = WSynchro.source.map.getView().getRotation();
  }
  var map = new ol.Map({
    target: 'map',
    view: new ol.View({
      zoom: z,
      center: pt,
      rotation: rot
    }),
    layers: [stamen]
  });

  // Open a new window
  function openWin() { WSynchro.open(); };
  
  // synchro count
  var nb = 0;
  // Synchronize function => set map position
  WSynchro.on ('synchronize', function(e){
    var view = map.getView();
    // Tell we are synchro
    if (e.pt == view.getCenter() && e.z == view.getZoom() && e.rot == view.getRotation()) {
      WSynchro.synchronize();
    } else {
      // Set position (will call WSynchro.synchronize on move end)
    	view.setCenter(e.pt);
      view.setZoom(e.z);
      view.setRotation(e.rot||0);
      $("#nb").text(++nb);
    }
  });

  // Synchronize on move / rotate
  map.on(['moveend','rotateend'], function(e) {
    var view = map.getView();
    WSynchro.synchronize({ pt:view.getCenter(), z:view.getZoom(), rot:view.getRotation() });
  });

  </script>
  
</body>
</html>